Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
resolve-package-path
Advanced tools
a special purpose fast memoizing way to resolve a node modules package.json
The resolve-package-path npm package is used to resolve the path of a package in the node_modules directory. It helps in finding the exact location of a package, which can be useful for various purposes such as loading configurations, plugins, or dependencies dynamically.
Resolve package path
This feature allows you to resolve the path of a given package from the node_modules directory. In this example, it resolves the path of the 'express' package from the current working directory.
const resolvePackagePath = require('resolve-package-path');
const packagePath = resolvePackagePath('express', process.cwd());
console.log(packagePath);
Resolve package path with custom base directory
This feature allows you to resolve the path of a package from a custom base directory. In this example, it resolves the path of the 'express' package from a specified custom base directory.
const resolvePackagePath = require('resolve-package-path');
const packagePath = resolvePackagePath('express', '/path/to/custom/base/directory');
console.log(packagePath);
Resolve package path with cache
This feature allows you to resolve the path of a package with caching. The cache helps in improving performance by storing previously resolved paths. In this example, it resolves the path of the 'express' package with a cache.
const resolvePackagePath = require('resolve-package-path');
const cache = new Map();
const packagePath = resolvePackagePath('express', process.cwd(), cache);
console.log(packagePath);
The 'resolve' package is used to resolve the path of a module like Node's require.resolve() but with more features. It can resolve paths based on custom file extensions, directories, and more. Compared to resolve-package-path, 'resolve' offers more flexibility and options for resolving module paths.
The 'pkg-dir' package finds the root directory of a Node.js project or a package. It is useful for locating the root directory of a project or a package, which can then be used to resolve paths relative to that root. While resolve-package-path focuses on resolving the path of a specific package, 'pkg-dir' helps in finding the root directory of a project or package.
The 'find-up' package is used to find a file or directory by walking up parent directories. It is useful for locating configuration files or other resources that may be located in parent directories. Compared to resolve-package-path, 'find-up' is more general-purpose and can be used to find any file or directory, not just package paths.
This project is special-purpose, made to resolve package.json
files for:
It cannot and does not resolve anything else.
To achieve its file-resolution performance, it does two specific things:
It memoizes results identically to node's require
. Specifically,
for a given moduleName and baseDir it will, for the duration of the process,
always return the exact same response.
It re-implements the parts of require.resolve
needed to resolve package.json
files ONLY. This removes unneeded I/O. (based on @davecombs approach)
yarn add resolve-package-path
const resolvePackagePath = require('resolve-package-path');
resolvePackagePath('rsvp', 'base-dir/to/start/the/node_resolution-algorithm-from') // => /path/to/rsvp.json or null
const { findUpPackagePath } = resolvePackagePath;
findUpPackagePath('base-dir/to/start') // => path/to/package.json or null
Node supports --preserve-symlinks
and NODE_PRESERVE_SYMLINKS=1
for compatibility this library respects these.
Although by default resolve-package-path
caches or memoizes results, this feature can be disabled:
const resolvePackagePath = require('resolve-package-path');
resolvePackagePath('rsvp', 'base-dir/to/start/the/node_resolution-algorithm-from', false) // => uncached result /path/to/rsvp.json or null
const { findUpPackagePath } = resolvePackagePath;
findUpPackagePath('base-dir/to/start', false) // => path/to/package.json or null
const resolvePackagePath = require('resolve-package-path');
resolvePackagePath._resetCache();
In some advanced circumtances, you may want to gain access to the cache to share between more systems. In that case, a cache instance of the following form can be provided as a third argument:
cache = {
RESOLVED_PACKAGE_PATH: new Map(),
REAL_FILE_PATH: new Map(),
REAL_DIRECTORY_PATH: new Map(),
};
findUpCache = new Map();
const resolvePackagePath = require('resolve-package-path');
resolvePackagePath('rsvp', 'path/to/start/from', cache);
const { findUpPackagePath } = resolvePackagePath;
findUpPackagePath('base-dir/to/start', findUpCache) // => path/to/package.json or null
For consumers who also do getRealFilePath
or
getRealDirectoryPath
calls on relevant paths, we expose them as utilities.
These utilties ensure identical functionality to resolve-package-path, and a
shared cache, which may help reduce IO.
FAQs
a special purpose fast memoizing way to resolve a node modules package.json
The npm package resolve-package-path receives a total of 910,706 weekly downloads. As such, resolve-package-path popularity was classified as popular.
We found that resolve-package-path demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.